package com.hbjycl.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hbjycl.entity.Admin;
import com.hbjycl.service.AdminService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController extends BaseController
{
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    @Resource
    private AdminService        adminService;
    
    /**
     * 前台首页
     * 
     * @param session
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index(HttpSession session, HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        Admin admin =adminService.getAdminByUsername("hbjycl");
        mav.addObject("admin", admin);
        return mav;
    }
    
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView login(Admin formAdmin,
            @RequestParam(value = "password", required = false, defaultValue = "") String password,
            @RequestParam(value = "username", required = false, defaultValue = "") String username)
    {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("welcome");
        Admin admin =adminService.getAdminByUsername(username);
        if(admin.getPassword().equals(password))
        {
        mav.addObject("admin", admin);
        }
        else
        {
        	mav.setViewName("fail");
        }
        return mav;
    }
    
}